package leetcode.code0819;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public String mostCommonWord(String paragraph, String[] banned) {
		Map<String, Integer> hash = new HashMap<>();
		Set<String> bans = new HashSet<>();
		for (String ban : banned) {
			bans.add(ban);
		}
		// !?',;.
		StringBuffer word = new StringBuffer();
		char[] cs = paragraph.toCharArray();
		int max = 0;
		String ans = null;
		for (int i = 0; i <= cs.length; i++) {
			char c = i < cs.length ? cs[i] : ' ';
			if (isStopChar(c)) {
				if(word.length() > 0) {
					String w = word.toString().toLowerCase();
					if (!bans.contains(w)) {
						int size = hash.getOrDefault(w, 0) + 1;
						if (size > max) {
							ans = w;
							max = size;
						}
						hash.put(w, size);
					}
					word = new StringBuffer();
				}
			} else {
				word.append(c);
			}
		}
		return ans;
	}

	private boolean isStopChar(char c) {
		return c == ' ' || c == '!' || c == '?' || c == '\'' || c == ',' || c == ';' || c == '.';
	}

	@Override
	public void debug4() {
		HelpDebug.compare("ball", this.mostCommonWord("Bob. hIt, baLl", HelpDebug.str2array2("[\"bob\",\"hit\"]")));
		HelpDebug.compare("ball", this.mostCommonWord("Bob hit a ball, the hit BALL flew far after it was hit.",HelpDebug.str2array2("[\"hit\"]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
